<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Python 交互式编程 - Adbean&#39;s Blog</title><meta name="Description" content="Adbean&#39;s Blog"><meta property="og:title" content="Python 交互式编程" />
<meta property="og:description" content="实验目的 了解一种“解释型”语言 python 使用 python 做一些简单的科学计算 实验环境 编程工具：Python Anaconda vscode 操作系统：Windows 环境准备 安装 Anaconda Anaconda 是一个可" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://adbean.gitee.io/posts/my-sixth-project/" />
<meta property="og:image" content="https://adbean.gitee.io/logo.png"/>
<meta property="article:published_time" content="2020-11-14T16:18:49+08:00" />
<meta property="article:modified_time" content="2020-11-14T16:18:49+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://adbean.gitee.io/logo.png"/>

<meta name="twitter:title" content="Python 交互式编程"/>
<meta name="twitter:description" content="实验目的 了解一种“解释型”语言 python 使用 python 做一些简单的科学计算 实验环境 编程工具：Python Anaconda vscode 操作系统：Windows 环境准备 安装 Anaconda Anaconda 是一个可"/>
<meta name="application-name" content="Adbean&#39;s Blog">
<meta name="apple-mobile-web-app-title" content="Adbean&#39;s Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="icon" href="../../Owl.ico"><link rel="apple-touch-icon" sizes="180x180" href="../../apple-touch-icon.png"><link rel="mask-icon" href="../../safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="../../site.webmanifest"><link rel="canonical" href="https://adbean.gitee.io/posts/my-sixth-project/" /><link rel="prev" href="https://adbean.gitee.io/posts/my-tenth-post/" /><link rel="next" href="https://adbean.gitee.io/posts/my-seventh-project/" /><link rel="stylesheet" href="../../lib/normalize/normalize.min.css"><link rel="stylesheet" href="../../css/style.min.css"><link rel="stylesheet" href="../../lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="../../lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Python 交互式编程",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/adbean.gitee.io\/posts\/my-sixth-project\/"
        },"genre": "posts","keywords": "项目, Python, 数学","wordcount":  1293 ,
        "url": "https:\/\/adbean.gitee.io\/posts\/my-sixth-project\/","datePublished": "2020-11-14T16:18:49+08:00","dateModified": "2020-11-14T16:18:49+08:00","publisher": {
            "@type": "Organization",
            "name": "Adbean"},"author": {
                "@type": "Person",
                "name": "Adbean"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('dark' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'dark' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="../../"> 主页 </a><a class="menu-item" href="../../posts/"> 文章 </a><a class="menu-item" href="../../posts/my-index/"> 索引 </a><a class="menu-item" href="../../tags/"> 标签 </a><a class="menu-item" href="../../categories/"> 分类 </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><a class="menu-item" href="../../" title="">主页</a><a class="menu-item" href="../../posts/" title="">文章</a><a class="menu-item" href="../../posts/my-index/" title="">索引</a><a class="menu-item" href="../../tags/" title="">标签</a><a class="menu-item" href="../../categories/" title="">分类</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">Python 交互式编程</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://adbean.gitee.io/" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>Adbean</a></span>&nbsp;<span class="post-category">收录于 <a href="../../categories/%E9%A1%B9%E7%9B%AE/"><i class="far fa-folder fa-fw"></i>项目</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-11-14">2020-11-14</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1293 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 3 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#实验目的">实验目的</a></li>
    <li><a href="#实验环境">实验环境</a>
      <ul>
        <li><a href="#环境准备">环境准备</a></li>
      </ul>
    </li>
    <li><a href="#使用-python-作为计算器">使用 python 作为计算器</a>
      <ul>
        <li><a href="#解释型语言">解释型语言</a></li>
        <li><a href="#数学库">数学库</a></li>
      </ul>
    </li>
    <li><a href="#使用-python-做高数题目">使用 Python 做高数题目</a>
      <ul>
        <li><a href="#高数工具">高数工具</a></li>
        <li><a href="#线性代数工具">线性代数工具</a></li>
      </ul>
    </li>
    <li><a href="#用-python-做高数线代作业">用 Python 做高数/线代作业</a>
      <ul>
        <li><a href="#高数">高数</a></li>
        <li><a href="#线性代数">线性代数</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="实验目的">实验目的</h2>
<ol>
<li>了解一种“<strong>解释型</strong>”语言 python</li>
<li>使用 python 做一些<strong>简单的科学计算</strong></li>
</ol>
<h2 id="实验环境">实验环境</h2>
<ol>
<li>编程工具：Python Anaconda vscode</li>
<li>操作系统：Windows</li>
</ol>
<h3 id="环境准备">环境准备</h3>
<h4 id="安装">安装</h4>
<h5 id="anaconda">Anaconda</h5>
<p><strong>Anaconda</strong> 是一个可用于<strong>科学计算</strong>的 Python 发行版</p>
<p><strong>Python</strong> 是一门编程语言，使用时，还有很多其他的配套工作，比如运行脚本、下载各种需要用到的<strong>库</strong>、<strong>管理环境</strong>等。</p>
<p>Anaconda 就把这些功能全都集成好了，省去很多琐碎的工作。总地来说，Anaconda 管理在使用 Python 时用到的<strong>包和环境</strong>。</p>
<p>Anaconda 的安装可以在<strong>官网</strong> <a href="https://www.anaconda.com/">https://www.anaconda.com/</a> 找到</p>
<p>同时国内也可以在<strong>清华镜像</strong>中下载 <a href="https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/">https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/</a></p>
<h5 id="python">Python</h5>
<p>安装 Anaconda 之后，python 同时也即安装好了</p>
<ol>
<li>Anaconda 使用 spyder 集成环境</li>
<li>Python 提供了许多启动方法，可以在 python 安装目录下直接运行如下：</li>
</ol>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../python_cmd.png"
        data-srcset="../../python_cmd.png, ../../python_cmd.png 1.5x, ../../python_cmd.png 2x"
        data-sizes="auto"
        alt="/python_cmd.png"
        title="python_cmd" /></p>
<h2 id="使用-python-作为计算器">使用 python 作为计算器</h2>
<h3 id="解释型语言">解释型语言</h3>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="o">&gt;&gt;&gt;</span> <span class="mi">2</span><span class="o">+</span><span class="mi">3</span>
<span class="mi">5</span>
<span class="o">&gt;&gt;&gt;</span><span class="nb">sum</span> <span class="o">=</span> <span class="mi">2</span><span class="o">+</span><span class="mi">3</span>
<span class="o">&gt;&gt;&gt;</span><span class="nb">sum</span>
<span class="mi">5</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../python_cal.png"
        data-srcset="../../python_cal.png, ../../python_cal.png 1.5x, ../../python_cal.png 2x"
        data-sizes="auto"
        alt="/python_cal.png"
        title="python_cal" /></p>
<h3 id="数学库">数学库</h3>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../python_math.png"
        data-srcset="../../python_math.png, ../../python_math.png 1.5x, ../../python_math.png 2x"
        data-sizes="auto"
        alt="/python_math.png"
        title="python_math" /></p>
<p><strong>import</strong> math 表示导入一个函数包（库）math，这个库有常用的数学函数，例如 sin，常数 pi 和 e 等。</p>
<p>使用时使用 <strong>库名.函数</strong> 与 <strong>库名.变量</strong> 。</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../python_dir.png"
        data-srcset="../../python_dir.png, ../../python_dir.png 1.5x, ../../python_dir.png 2x"
        data-sizes="auto"
        alt="/python_dir.png"
        title="python_dir" /></p>
<p>使用 <strong>dir()</strong> 函数返回 math 库所含有的<strong>属性和方法列表</strong></p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../python_help.png"
        data-srcset="../../python_help.png, ../../python_help.png 1.5x, ../../python_help.png 2x"
        data-sizes="auto"
        alt="/python_help.png"
        title="python_help" /></p>
<p>使用 <strong>help()</strong> 函数用于查看函数或模块<strong>用途</strong>的详细说明</p>
<p>python 是拥有世界上最庞大的<strong>函数库（程序库）的语言</strong>。</p>
<p>这归功于 Python 是<strong>最早开源</strong>的项目，众多大学非计算机/计算机专业使用 python 作为科学计算工具。</p>
<p>得益于开放和巨大且日益增长的程序库，今天从数学函数、到 <strong>web 编程</strong>、网络分析、<strong>数据挖掘</strong>、<strong>机器学习</strong>、生物信息处理、图形图像、大数据处理等等，python 都是最重要、最方便的开发语言。</p>
<h2 id="使用-python-做高数题目">使用 Python 做高数题目</h2>
<h3 id="高数工具">高数工具</h3>
<h4 id="sympy">SymPy</h4>
<p>sympy 是一个 Python 的科学计算库，用一套强大的符号计算体系完成诸如多项式求值、求极限、解方程、求积分、微分方程、级数展开、矩阵运算等等计算问题</p>
<p><strong>安装</strong>：</p>
<p>通过在 powershell 或是 cmd 使用 pip install sympy 安装 sympy 模块</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../pip_sympy.png"
        data-srcset="../../pip_sympy.png, ../../pip_sympy.png 1.5x, ../../pip_sympy.png 2x"
        data-sizes="auto"
        alt="/pip_sympy.png"
        title="pip_sympy" /></p>
<p><strong>官方教程</strong>：</p>
<p><a href="https://docs.sympy.org/latest/tutorial/index.html">https://docs.sympy.org/latest/tutorial/index.html</a></p>
<h5 id="表达式与表达式求值">表达式与表达式求值</h5>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
<span class="n">fx</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">*</span> <span class="n">y</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">fx</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">subs</span><span class="o">=</span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="mi">4</span><span class="p">})</span>
<span class="k">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../sympy_evalf.png"
        data-srcset="../../sympy_evalf.png, ../../sympy_evalf.png 1.5x, ../../sympy_evalf.png 2x"
        data-sizes="auto"
        alt="/sympy_evalf.png"
        title="sympy_evalf" /></p>
<h5 id="函数方程求解">函数方程求解</h5>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
<span class="n">fx</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">9</span>
<span class="k">print</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">fx</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../sympy_solve.png"
        data-srcset="../../sympy_solve.png, ../../sympy_solve.png 1.5x, ../../sympy_solve.png 2x"
        data-sizes="auto"
        alt="/sympy_solve.png"
        title="sympy_solve" /></p>
<h5 id="极限">极限</h5>
<p>$$ \lim\limits_{x\to0} \frac{sinx}{x}  = 1 $$
$$ \lim\limits_{x\to0} (1+x)^{\frac{1}{x}} = e $$
$$ \lim\limits_{x\to0} (1+\frac{1}{x})^{x} = e $$</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">f1</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="n">x</span>
<span class="n">f2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">)</span>
<span class="n">f3</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="n">x</span>
<span class="c1"># limit(expression, symbol, the value toward which z tends)</span>
<span class="n">lim1</span> <span class="o">=</span> <span class="n">limit</span><span class="p">(</span><span class="n">f1</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">lim2</span> <span class="o">=</span> <span class="n">limit</span><span class="p">(</span><span class="n">f2</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">lim3</span> <span class="o">=</span> <span class="n">limit</span><span class="p">(</span><span class="n">f3</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">oo</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">lim1</span><span class="p">,</span> <span class="n">lim2</span><span class="p">,</span> <span class="n">lim3</span><span class="p">)</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../sympy_limits.png"
        data-srcset="../../sympy_limits.png, ../../sympy_limits.png 1.5x, ../../sympy_limits.png 2x"
        data-sizes="auto"
        alt="/sympy_limits.png"
        title="sympy_limits" /></p>
<h3 id="线性代数工具">线性代数工具</h3>
<h4 id="numpy">Numpy</h4>
<p><strong>Numpy</strong> 是<strong>向量计算</strong>的基础库，Numpy 是一个很重要、使用很多的科学计算包。在 Python 领域里有很多的与科学计算相关的包都或多或少地都使用到了 Numpy 这个模块包，例如著名的大数据包 pandas 就是基于 numpy</p>
<p><strong>安装</strong>：</p>
<p>通过在 powershell 或是 cmd 使用 pip install numpy 安装 numpy 模块</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../numpy_install.png"
        data-srcset="../../numpy_install.png, ../../numpy_install.png 1.5x, ../../numpy_install.png 2x"
        data-sizes="auto"
        alt="/numpy_install.png"
        title="numpy_install" /></p>
<p><strong>官方教程</strong>：</p>
<p><a href="https://numpy.org/doc/stable/">https://numpy.org/doc/stable/</a></p>
<h5 id="求方程组的解">求方程组的解</h5>
<p>$ \begin{bmatrix} 1 &amp; 1 &amp; 1 \\ 0 &amp; 2 &amp; 5 \\ 2 &amp; 5 &amp; -1 \end{bmatrix} $ $ \begin{bmatrix} x \\ y \\ z \end{bmatrix} $ $ = $ $ \begin{bmatrix} 6 \\ -4 \\ 27 \end{bmatrix} $</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">27</span><span class="p">]])</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="n">b</span><span class="p">))</span>
</code></pre></div><p>其中 allclose 函数返回 True 如果两个向量在<strong>容差</strong>内是相等的</p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../numpy_axb.png"
        data-srcset="../../numpy_axb.png, ../../numpy_axb.png 1.5x, ../../numpy_axb.png 2x"
        data-sizes="auto"
        alt="/numpy_axb.png"
        title="numpy_axb" /></p>
<h5 id="矩阵乘法">矩阵乘法</h5>
<p>$ \begin{bmatrix} 1 &amp; 2 &amp; 3 \\ 4 &amp; 5 &amp; 6 \end{bmatrix} $ $ \begin{bmatrix} 2 &amp; 3 &amp; 4 \\ 5 &amp; 6 &amp; 7 \\ 8 &amp; 9 &amp; 10 \end{bmatrix} $ $ = $ $ \begin{bmatrix} 36 &amp; 42 &amp; 48 \\ 81 &amp; 96 &amp; 111 \end{bmatrix} $</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">11</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>
</code></pre></div><p>其中 matmul 和 dot 函数都能实现<strong>矩阵乘法</strong></p>
<p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../numpy_dot.png"
        data-srcset="../../numpy_dot.png, ../../numpy_dot.png 1.5x, ../../numpy_dot.png 2x"
        data-sizes="auto"
        alt="/numpy_dot.png"
        title="numpy_dot" /></p>
<h2 id="用-python-做高数线代作业">用 Python 做高数/线代作业</h2>
<h3 id="高数">高数</h3>
<h4 id="求极限">求极限</h4>
<p>$$ \lim\limits_{x\to0^+} (\frac{1}{\sqrt{x}}) ^{tanx}  = ? $$</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">f1</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="o">**</span> <span class="n">tan</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">lim1</span> <span class="o">=</span> <span class="n">limit</span><span class="p">(</span><span class="n">f1</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="s2">&#34;+&#34;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">lim1</span><span class="p">)</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../limits1_sympy.png"
        data-srcset="../../limits1_sympy.png, ../../limits1_sympy.png 1.5x, ../../limits1_sympy.png 2x"
        data-sizes="auto"
        alt="/limits1_sympy.png"
        title="limits1_sympy" /></p>
<h4 id="求导数">求导数</h4>
<p>$$ y = e^x cos x, 求 d^2y $$</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">dy</span> <span class="o">=</span> <span class="n">diff</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="n">d2y</span> <span class="o">=</span> <span class="n">diff</span><span class="p">(</span><span class="n">dy</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">d2y</span><span class="p">)</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../diff1_sympy.png"
        data-srcset="../../diff1_sympy.png, ../../diff1_sympy.png 1.5x, ../../diff1_sympy.png 2x"
        data-sizes="auto"
        alt="/diff1_sympy.png"
        title="diff1_sympy" /></p>
<h3 id="线性代数">线性代数</h3>
<p>$$ A = \begin{bmatrix} 0 &amp; 1 &amp; 3 \\ 1 &amp; -1 &amp; 0 \\ -1 &amp; 2 &amp; 1 \end{bmatrix} 的逆矩阵 A^{-1} $$</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../inv_numpy.png"
        data-srcset="../../inv_numpy.png, ../../inv_numpy.png 1.5x, ../../inv_numpy.png 2x"
        data-sizes="auto"
        alt="/inv_numpy.png"
        title="inv_numpy" /></p>
<p>$$ 方程组\\ x_1 + x_2 + x_3 + x_4 = 1 \\ 2x_1 + 3x_2 + 4x_3 + 5x_4 = 5 \\ 4x_1 + 9x_2 + 16x_3 + 25x_4 = 25 \\ 8x_1 + 27x_2 + 64x_3 + 125x_4 = 125 \\ 有解() $$</p>
<div class="highlight"><pre class="chroma"><code class="language-Python" data-lang="Python"><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">],[</span><span class="mi">4</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">25</span><span class="p">],[</span><span class="mi">8</span><span class="p">,</span><span class="mi">27</span><span class="p">,</span><span class="mi">64</span><span class="p">,</span><span class="mi">125</span><span class="p">]])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">5</span><span class="p">],[</span><span class="mi">25</span><span class="p">],</span> <span class="p">[</span><span class="mi">125</span><span class="p">]])</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="n">b</span><span class="p">))</span>
</code></pre></div><p><img
        class="lazyload"
        src="../../svg/loading.min.svg"
        data-src="../../axb_numpy.png"
        data-srcset="../../axb_numpy.png, ../../axb_numpy.png 1.5x, ../../axb_numpy.png 2x"
        data-sizes="auto"
        alt="/axb_numpy.png"
        title="axb_numpy" /></p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-11-14</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="../../tags/%E9%A1%B9%E7%9B%AE/">项目</a>,&nbsp;<a href="../../tags/python/">Python</a>,&nbsp;<a href="../../tags/%E6%95%B0%E5%AD%A6/">数学</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="../../">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="../../posts/my-tenth-post/" class="prev" rel="prev" title="10-数据结构与算法设计"><i class="fas fa-angle-left fa-fw"></i>10-数据结构与算法设计</a>
            <a href="../../posts/my-seventh-project/" class="next" rel="next" title="IT 伦理与道德 - 信息爆炸时代，如何保护你的个人信息安全">IT 伦理与道德 - 信息爆炸时代，如何保护你的个人信息安全<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.74.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://adbean.gitee.io/" target="_blank">Adbean</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="../../lib/katex/katex.min.css"><link rel="stylesheet" href="../../lib/katex/copy-tex.min.css"><script type="text/javascript" src="../../lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="../../lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="../../lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="../../lib/katex/katex.min.js"></script><script type="text/javascript" src="../../lib/katex/auto-render.min.js"></script><script type="text/javascript" src="../../lib/katex/copy-tex.min.js"></script><script type="text/javascript" src="../../lib/katex/mhchem.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false}};</script><script type="text/javascript" src="../../js/theme.min.js"></script></body>
</html>
